package apple.encryptedNode.adapter;

import it.telecomitalia.jchat.R;

import java.util.ArrayList;



import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import apple.encryptedNode.vo.Node;
/***
 * 秘密箱列表适配器
 * **/
public class NodeAdapter extends ArrayAdapter<Node> {
	private final String TAG = NodeAdapter.class.getSimpleName();
	
	private LayoutInflater inflater;
	protected ArrayList<Node> items;
	/**
	 * 构造函数
	 * @param context
	 * @param layout
	 * @param items
	 */
    public NodeAdapter(Context context, int layout, ArrayList<Node> items) {
        super(context, layout, items);
        Log.d(TAG, "NodeAdapter");
        inflater = LayoutInflater.from(context);
        this.items = items;
    }
    
    @Override
    public int getCount() {
        return items.size();
    }
    
    @Override
    public synchronized void clear() {
        items.clear();
        notifyDataSetChanged();
    }
    
    @Override
    public synchronized void add(Node bean) {
    	items.add(bean);
    }
    
	public synchronized void modify(int index, Node bean){
		items.set(index, bean);
	}
	
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
    	Log.d(TAG, "getView");
    	ViewHolder holder;

        if (convertView == null) {
        	convertView = inflater.inflate(R.layout.note_list_component, null);
        	
        	holder = new ViewHolder();
        	holder.topicName = (TextView) convertView.findViewById(R.id.topicName);
        	holder.topicContent = (TextView) convertView.findViewById(R.id.topicContent);
        	
        	holder.encryptionStatus=(TextView) convertView.findViewById(R.id.encryptionStatus);
        	convertView.setTag(holder);
        }else{
        	holder = (ViewHolder) convertView.getTag();
        }
        
        Node bean = items.get(position);
        if (bean != null) {
        	holder.topicName.setText(bean.getTopicName());        	
        	holder.topicContent.setText(bean.getTopicContent());
        	Log.d(TAG, bean.getEncryptionStatus());
        	if(bean.getEncryptionStatus().equals("y"))
        	{
        		holder.encryptionStatus.setText("已加密");
        	}else if(bean.getEncryptionStatus().equals("n"))
        	{
        		holder.encryptionStatus.setText("已解密");
        	}
        	
        }
        return convertView;
    }
    
    private static class ViewHolder{
    	TextView topicName;
    	TextView topicContent;
    	TextView encryptionStatus;
    }
}
